package com.itcast.googleplay.holder;

import com.itcast.googleplay.R;
import com.itcast.googleplay.domain.AppInfo;
import com.itcast.googleplay.http.CZApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AppSafeHolder extends BaseHolder<AppInfo>{
	private ImageView[] image ;
	private LinearLayout[] linearLayout ;
	private ImageView[] imageDes;
	private TextView[] textView ;
	private DisplayImageOptions options;
	private LinearLayout llExpend;
	private LinearLayout llRoot;
	public boolean isOpen = false;
	private LayoutParams layoutParams;
	private ImageView ivArrow;
	@Override
	public void initData() {
		AppInfo appInfo = getData();
		
		for (int i = 0; i < 3; i++) {
//			0 1	2		3
//			0 1			2
//			0			1
//			不能取值		0
			if(i<appInfo.safe.size()){
				image[i].setVisibility(View.VISIBLE);
				linearLayout[i].setVisibility(View.VISIBLE);
				//填充描述内容
				textView[i].setText(appInfo.safe.get(i).safeDes);
				//填充描述内容左侧的图片
				if(!TextUtils.isEmpty(appInfo.safe.get(i).safeDesUrl)){
					ImageLoader.getInstance().displayImage
						(CZApi.BASEIMGURL+appInfo.safe.get(i).safeDesUrl,imageDes[i],options);
				}
				if(!TextUtils.isEmpty(appInfo.safe.get(i).safeUrl)){
					ImageLoader.getInstance().displayImage
						(CZApi.BASEIMGURL+appInfo.safe.get(i).safeUrl,image[i],options);
				}
			}else{
				image[i].setVisibility(View.GONE);
				linearLayout[i].setVisibility(View.GONE);
			}
		}
		//测量完整展示的高度
		getLongHeight();
		//不展示高度 就是0
		//一进入详情界面时,就让可扩展的linearLayout不显示里面的条目,高度指定成0
		layoutParams = llExpend.getLayoutParams();
		layoutParams.height = 0;
		llExpend.setLayoutParams(layoutParams);
		//扩展动画()
//		ValueAnimator
	}

	private int getLongHeight() {
		//让系统测量可以扩展的线性布局的高度
		llExpend.measure(0, 0);
		return llExpend.getMeasuredHeight();
	}

	@Override
	public View initView() {
		View rootView = UIUtils.inflate(R.layout.layout_detial_app_safe);
		llRoot = (LinearLayout) rootView.findViewById(R.id.llRoot);
		
		llRoot.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//实现扩展动画
				expend();
			}
		});
		
		llExpend = (LinearLayout) rootView.findViewById(R.id.ll_expend);
		ivArrow = (ImageView) rootView.findViewById(R.id.iv_arrow);
		
		//先给数组分配空间,然后让数组在赋值过程中使用
		image = new ImageView[3];
		image[0] = (ImageView) rootView.findViewById(R.id.ivsafe1);
		image[1] = (ImageView) rootView.findViewById(R.id.ivsafe2);
		image[2] = (ImageView) rootView.findViewById(R.id.ivsafe3);
		
		linearLayout = new LinearLayout[3];
		linearLayout[0] = (LinearLayout) rootView.findViewById(R.id.ll1);
		linearLayout[1] = (LinearLayout) rootView.findViewById(R.id.ll2);
		linearLayout[2] = (LinearLayout) rootView.findViewById(R.id.ll3);

		imageDes = new ImageView[3];
		imageDes[0] = (ImageView)rootView.findViewById(R.id.iv_des1);
		imageDes[1] = (ImageView)rootView.findViewById(R.id.iv_des2);
		imageDes[2] = (ImageView)rootView.findViewById(R.id.iv_des3);
		
		textView = new TextView[3];
		textView[0] = (TextView) rootView.findViewById(R.id.tv_des1);
		textView[1] = (TextView) rootView.findViewById(R.id.tv_des2);
		textView[2] = (TextView) rootView.findViewById(R.id.tv_des3);
		
		//初始图片缓存方式代码
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_default)//在加载图片的时候,展示的默认图片
				
				.showImageForEmptyUri(R.drawable.ic_default)//uri传递出现问题时展示的图片
				
				.showImageOnFail(R.drawable.ic_default)//加载网络失败的时候,展示的图片
				
				.cacheInMemory(true)//是否需要内存缓存
				
				.cacheOnDisk(true)//是否需要磁盘缓存
				
				.considerExifParams(true)//是否支持图片自动调整,让其正常图像显示
		
				.build();//设置圆角半径
		
		return rootView;
	}

	protected void expend() {
		//1.扩展(0---->默认高度) 收缩(默认高度------->0)
		ValueAnimator valueAnimator = null;
		if(!isOpen){
			//2.点之前是收缩的,点击后扩展
			isOpen = true;
			//属性动画,值动画
			valueAnimator = ValueAnimator.ofInt(0,getLongHeight());
		}else{
			//3.当前是扩展的,点击后收缩
			isOpen = false;
			valueAnimator = ValueAnimator.ofInt(getLongHeight(),0);
		}
		if(valueAnimator!=null){
			//4.对扩展过程做事件监听(时刻监听控件的高度值)
			valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
				@Override
				public void onAnimationUpdate(ValueAnimator valueAnimator) {
					//5.此方法的valueAnimator就存储了控件的每时每刻的高度大小
					
					//100px
					//1  10px	
					//2	 10px
					//3  10px
					int height = (Integer) valueAnimator.getAnimatedValue();
					layoutParams.height = height;
					llExpend.setLayoutParams(layoutParams);
				}
			});
			//5,对动画做是否完成的事件监听
			valueAnimator.addListener(new AnimatorListener() {
				
				@Override
				public void onAnimationStart(Animator arg0) {
					
				}
				
				@Override
				public void onAnimationRepeat(Animator arg0) {
					
				}
				
				@Override
				public void onAnimationEnd(Animator arg0) {
					if(isOpen){
						//扩展开,朝上
						ivArrow.setBackgroundResource(R.drawable.arrow_up);
					}else{
						//关闭,朝下
						ivArrow.setBackgroundResource(R.drawable.arrow_down);
					}
				}
				
				@Override
				public void onAnimationCancel(Animator arg0) {
					
				}
			});
			
			valueAnimator.setDuration(300);
			valueAnimator.start();
		}
		
	}
}
